package dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import model.ObjectPojo;
import model.PropPojo;

public class TripleDao {
	
	public void insertTripleDao(ObjectPojo sub, PropPojo pro, ObjectPojo obj){
		
		//check that the items exist
		ObjectDao o = new ObjectDao();
		boolean doesSubExist = o.doesObjectExist(sub);
		PropDao p = new PropDao();
		boolean doesPropExist = p.doesPropExist(pro);
		boolean doesObjExist = o.doesObjectExist(obj);

System.out.println(doesSubExist + " - " + doesPropExist + " - " + doesObjExist);
		
		int subId;
		int proId;
		int objId;
		
		
		if(doesSubExist){
			subId = o.getObjectId(sub.getObjectUri());
		} 
			else{
				o.insertObjectPojo(sub);
				subId = o.getObjectId(sub.getObjectUri());
			}
		
		if(doesPropExist){
			proId = p.getPropId(pro.getPropUri());
		}
			else{
				p.insertPropPojo(pro);
				proId = p.getPropId(pro.getPropUri());
			}
		
		if(doesObjExist){
			objId = o.getObjectId(obj.getObjectUri());
		}
			else{
				o.insertObjectPojo(obj);
				objId = o.getObjectId(obj.getObjectUri());			
			}
		
		// all the items have been added, so add the triple
		//check that it doesn't already exist
		
		try {
			DBCon db = new DBCon();
			Connection con = db.getCon();
			Statement stmt = con.createStatement();
			// check that the triple doesn't already exist
			
				ResultSet rs = stmt.executeQuery("SELECT triple_id FROM triples WHERE sub_id="+subId+" AND pro_id="+proId+" AND obj_id="+objId+"");
			if(!rs.next()){
				//  if the triple doesnt exist
				// add it
				stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, obj_id) VALUES("+subId+","+proId+","+objId+")");
			}
		
		
		} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void insertTripleDao(ObjectPojo sub, PropPojo pro, String obj){
	
		//check that the items exist
		ObjectDao o = new ObjectDao();
		boolean doesSubExist = o.doesObjectExist(sub);
		PropDao p = new PropDao();
		boolean doesPropExist = p.doesPropExist(pro);
		
		int subId;
		int proId;
			
		if(doesSubExist){
			subId = o.getObjectId(sub.getObjectUri());
		} 
			else{
				o.insertObjectPojo(sub);
				subId = o.getObjectId(sub.getObjectUri());
			}
		
		if(doesPropExist){
			proId = p.getPropId(pro.getPropUri());
		}
			else{
				p.insertPropPojo(pro);
				proId = p.getPropId(pro.getPropUri());
			}
		
		// all the items have been added, so add the triple
		//check that it doesn't already exist
		
		try {
			DBCon db = new DBCon();
			Connection con = db.getCon();
			Statement stmt = con.createStatement();
			// check that the triple doesn't already exist
			
				ResultSet rs = stmt.executeQuery("SELECT triple_id FROM triples WHERE sub_id="+subId+" AND pro_id="+proId+" AND text='"+obj+"'");
			if(!rs.next()){
				//  if the triple doesnt exist
				// add it
				stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, text) VALUES("+subId+","+proId+",'"+obj+"')");
			}
		
		} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}

	public void insertTripleDao(ObjectPojo sub, PropPojo pro, BigDecimal numObj){
		
		//check that the items exist
		ObjectDao o = new ObjectDao();
		boolean doesSubExist = o.doesObjectExist(sub);
		PropDao p = new PropDao();
		boolean doesPropExist = p.doesPropExist(pro);
		
		int subId;
		int proId;
			
		if(doesSubExist){
			subId = o.getObjectId(sub.getObjectUri());
		} 
			else{
				o.insertObjectPojo(sub);
				subId = o.getObjectId(sub.getObjectUri());
			}
		
		if(doesPropExist){
			proId = p.getPropId(pro.getPropUri());
		}
			else{
				p.insertPropPojo(pro);
				proId = p.getPropId(pro.getPropUri());
			}
		
		// all the items have been added, so add the triple
		//check that it doesn't already exist
		
		try {
			DBCon db = new DBCon();
			Connection con = db.getCon();
			Statement stmt = con.createStatement();
			// check that the triple doesn't already exist
			
//			String query = "SELECT triple_id FROM triples WHERE sub_id="+subId+" AND numeric="+numObj+" AND pro_id="+proId+" ";
			String query = "SELECT triple_id FROM triples WHERE sub_id = " + subId
			+ " AND pro_id = " + proId + " AND num = " + numObj
			+ "";
			
			ResultSet rs = stmt.executeQuery(query);
			if(!rs.next()){
				//  if the triple doesnt exist
				// add it
//				stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, num) VALUES("+subId+","+proId+","+numObj+")");
				stmt.executeUpdate("INSERT INTO triples(sub_id, pro_id, num) VALUES("+subId+","+proId+",78.9)");
						}
		
		} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}
}